require "ZhuYe";
require "util";
require "logUtil";
require "ShuYuan"
require "FuZhai"
require "GuanQia"
require "setting"
require "LaoFang"
require "XunFang"
require "FuBen"

function wangLuoLianJieShiBai()
    x, y = findMultiColorInRegionFuzzy(0xc56628, "150|-2|0xc06629,-35|125|0xfecf92,166|123|0xfecf92,-82|-218|0xfecf92,161|-218|0xfecf92,-54|-121|0xffeece,-42|-121|0xffeece,3|-122|0xffeece,43|-120|0xffeece,44|-111|0xffeece,68|-129|0xffeece,124|-117|0xffeece,124|-129|0xffeece,166|-122|0xffeece,176|-110|0xffeece,88|-105|0xffeece,26|-105|0xffeece,-4|-103|0xffeece,8|-103|0xffeece", 85, 32, 762, 1066, 1178, 0, 0)

    if x == -1 then
        x, y = findMultiColorInRegionFuzzy(0xbe652a, "147|-2|0xbc6429,-35|53|0x7f4515,188|54|0x814816,56|-106|0xffeece,-5|-114|0xffeece,34|-115|0xffeece,82|-98|0xffeece,99|-98|0xffeece,133|-97|0xffeece,133|-117|0xffeece,55|-114|0xffeece,111|-202|0xfecf92,-70|133|0x926e4a", 85, 18, 744, 1062, 1193, 0, 0)
    end

    if x==-1 then
        x, y = findMultiColorInRegionFuzzy(0xc36425,"-45|-17|0xbe6428,141|-10|0xbc6327,166|35|0x804716,-54|34|0x804516,1|-40|0x4e3017,102|-40|0x4e3017,189|-3|0x4e3017,99|54|0x4e3017,121|-5|0xc36425,42|-1|0xfff9d7,46|-5|0xfff9d7,22|2|0xfff9d7,72|10|0xfff9d7,39|19|0x833f17,56|8|0xb5591f,83|15|0x954a1b", 90, 379, 956, 698, 1080, 0, 0)
    end
    if x > -1 then
        click(x, y)
        logger("网络连接失败", LOG_DEBUG)
        return true
    end
end

function jingRuYouXi()

    x, y = findMultiColorInRegionFuzzy(0xc06a0c, "325|6|0xb7650b,58|-2|0xfdf8ca,139|20|0xfeec86,187|4|0xfdf5ba,239|10|0xfdf2a7,260|1|0xfdf7c2,-77|-1384|0xfed748,-38|-1410|0x000000,60|-1456|0xfdf592,81|-1346|0x000000,139|-1355|0xfee864,179|-1365|0xfdf285,275|-1432|0xfefdc7,250|-1399|0xfefe5e,357|-1386|0xfbf6b3,281|-1301|0xef890e,375|-1289|0xcc710d,350|-1319|0x000000,439|-1384|0x000000", 95, 139, 77, 1061, 1721, 0, 0)
		if x==-1 then
		x, y = findMultiColorInRegionFuzzy(0xfdf8ca,"-2|32|0x8e4005,-59|18|0x632400,-64|0|0xfdf8ca,-115|-54|0x020202,-88|82|0x020201,241|28|0x0c0c0b,179|5|0xb9710e,116|-1|0xfdf9cd,20|27|0xfeea78,-48|30|0xfee971,-34|-20|0xe18e23,128|-29|0xdf982d,-137|1|0xbf6c0c,62|6|0xfdf5ba,76|7|0xfdf4b6,111|0|0xfdf8ca,117|13|0xfdf1a3,133|13|0x632300,-21|8|0xb76009", 85, 296, 1501, 824, 1697, 0, 0)
if x > -1 then

end
		end
		
    if x > -1 then
        click(x, y)
        logger("进入游戏", LOG_DEBUG)
        return true
    end
end


function gongGaoGuanBi()

x, y = findMultiColorInRegionFuzzy(0xfee34b,"6|-26|0x180402,29|1|0x180402,-1|29|0x180402,-21|-1|0x180402,-329|-36|0xfefe75,-366|-2|0xc21f15,-483|7|0xda2518,-444|-26|0xbe1d15,-571|-52|0x750a07,-555|-42|0xfefe7e,-531|-52|0xfefe8b,-485|-46|0xfefe80,-468|-42|0xfefe7c,-462|-26|0xfee45a,-503|-21|0x850f0a,-417|-11|0xfebb3b,-330|-52|0xfefe84,-321|-12|0xfedc4e,-297|-18|0xbc1e15", 85, 315, 332, 1056, 522, 0, 0)
if x > -1 then
   click(x, y)
        logger("关闭公告", LOG_DEBUG)
        return true
end
end 

ZhuJieMian = {}


function ZhuJieMian.shuYuan()

    --[[    x, y = findMultiColorInRegionFuzzy(0xf3d17f, "-6|32|0xfeebb3,-1|71|0x221a15,-12|-30|0x130b08,-151|16|0x5f4035,-150|62|0x6b746e,104|75|0x6e7269,94|64|0x5c463f,85|23|0x58443d,-30|-47|0x7d867d,136|-16|0x505855,129|51|0x838376,-153|18|0x584234,16|149|0xa9ab99", 85, 34, 837, 1063, 1195, 0, 0)]]

    --[[    x, y = findMultiColorInRegionFuzzy(0x603e3b, "-309|191|0x140c0a,-156|350|0xab7c51,-111|630|0x431f14,-72|-60|0x150d09,-86|-602|0xb5c8e1,-147|-481|0xecf0ef,-220|-516|0xe6e9f0,-91|290|0x424544,-76|290|0x393d3a,-264|296|0x86846b,-256|383|0x888777,-212|16|0x767f77,-211|-29|0x5f4034,-207|624|0x7d827c", 85, 12, 375, 1060, 1649, 0, 0)]]

    x, y = findMultiColorInRegionFuzzy(0x727b75, "-109|-11|0x18110d,-111|-44|0x150d09,4|47|0x767d75,-178|-74|0x7c847c,-182|-309|0xcac096,-32|-227|0x707f84,-157|-313|0xcac096,-157|-313|0xcac096,-69|-172|0x87887d,-46|333|0x6f7166,-347|214|0x140c0a,-194|371|0xab7c51,-138|667|0x462015,-35|662|0x72766e,-239|657|0x73776e,-11|-231|0x707f84,44|-43|0x515855,113|66|0x5d605a,-274|88|0x7f694b", 85, 34, 533, 1040, 1677, 0, 0)


    if x > -1 then

        click(x, y)
        logger("主界面.书院", LOG_DEBUG)
        return true
    end

end

function ZhuJieMian.fuZhai()

    --[[    x, y = findMultiColorInRegionFuzzy(0xae7b52, "-156|-150|0x140c0a,-21|-172|0xa7a997,147|-37|0x6e7167,129|35|0x818070,66|-56|0x414443,-63|-57|0x3a3e3b,64|-56|0x434745,-80|-165|0x74766b,77|-166|0x74796f", 85, 7, 1142, 1064, 1413, 0, 0)
            ]]
    --[[    x, y = findMultiColorInRegionFuzzy(0x110b0a, "-150|-98|0x140c0a,3|61|0xab7c51,48|341|0x431f14,87|-349|0x150d09,73|-891|0xb5c8e1,12|-770|0xecf0ef,-61|-805|0xe6e9f0,68|1|0x424544,83|1|0x393d3a,-105|7|0x86846b,-97|94|0x888777,-53|-273|0x767f77,-52|-318|0x5f4034,-48|335|0x7d827c", 85, 12, 375, 1060, 1649, 0, 0)]]

    x, y = findMultiColorInRegionFuzzy(0x414543, "20|-324|0x18110d,18|-357|0x150d09,133|-266|0x767d75,-49|-387|0x7c847c,-53|-622|0xcac096,97|-540|0x707f84,-28|-626|0xcac096,-28|-626|0xcac096,60|-485|0x87887d,83|20|0x6f7166,-218|-99|0x140c0a,-65|58|0xab7c51,-9|354|0x462015,94|349|0x72766e,-110|344|0x73776e,118|-544|0x707f84,173|-356|0x515855,242|-247|0x5d605a,-145|-225|0x7f694b", 85, 34, 533, 1040, 1677, 0, 0)

    if x > -1 then
        click(x, y)
        logger("主界面.府宅", LOG_DEBUG)
        return true
    end
end
function ZhuJieMian.guanQia()
    --[[    x, y = findMultiColorInRegionFuzzy(0x401e14, "-110|-13|0x83857b,96|-9|0x73776d,-160|-25|0x43453c,-206|-27|0x696b61,160|-25|0x3d3d37,211|-120|0x020000,195|-219|0x020000,-133|-219|0x5f635d,129|-222|0x5e6762", 85, 20, 1364, 1063, 1700, 0, 0)]]

    --[[    x, y = findMultiColorInRegionFuzzy(0x787e76, "97|-159|0x020000,-159|-269|0xab7c51,-114|11|0x431f14,-75|-679|0x150d09,-89|-1221|0xb5c8e1,-150|-1100|0xecf0ef,-223|-1135|0xe6e9f0,-94|-329|0x424544,-79|-329|0x393d3a,-267|-323|0x86846b,-259|-236|0x888777,-215|-603|0x767f77,-214|-648|0x5f4034,-210|5|0x7d827c", 85, 12, 375, 1060, 1649, 0, 0)]]

    x, y = findMultiColorInRegionFuzzy(0x4a2116, "34|-656|0x18110d,32|-689|0x150d09,147|-598|0x767d75,-35|-719|0x7c847c,-39|-954|0xcac096,111|-872|0x707f84,-14|-958|0xcac096,-14|-958|0xcac096,74|-817|0x87887d,97|-312|0x6f7166,-204|-431|0x140c0a,-51|-274|0xab7c51,5|22|0x462015,108|17|0x72766e,-96|12|0x73776e,132|-876|0x707f84,187|-688|0x515855,256|-579|0x5d605a,-131|-557|0x7f694b", 85, 34, 533, 1040, 1677, 0, 0)

    if x > -1 then
        click(x, y)
        logger("主界面.关卡", LOG_DEBUG)
        return true
    end

end

function ZhuJieMian.fuBen()
    x, y = findMultiColorInRegionFuzzy(0x9d7f50, "268|-41|0x802521,359|79|0x140c09,362|128|0x1b130f,-37|-324|0x55564f,164|-165|0x464b49,-190|-336|0x6a797f,-49|-519|0xc5d4db,119|-413|0xcedce2,283|226|0x949481,-2|-265|0x453323", 90, 0, 463, 884, 1736, 0, 0)
    if x > -1 then
        click(x, y)
        logger("主界面.副本", LOG_DEBUG)
        return true
    end
end


function ZhuJieMian.laoFang()

    --[[    x, y = findMultiColorInRegionFuzzy(0x19110e, "10|-21|0xefcd70,-101|-4|0x4f5855,-54|57|0x444846,-103|63|0x353937,-72|77|0xeeb97e,-103|2|0x4f5855,-167|30|0xbfbfa9", 85, 94, 867, 1050, 1119, 0, 0)]]
    --[[    x, y = findMultiColorInRegionFuzzy(0x898b7f, "561|136|0x513b39,607|144|0x412927,412|-103|0xa5bfca,546|-650|0x9cb6d9,108|-584|0xdceaf5,87|-7|0x1a120f,222|27|0x616b66,13|44|0x9f6a65,-12|-20|0x555a55,78|-191|0x647276,213|-461|0xf1f5f8,119|-656|0xaccce5,478|-83|0x130b08", 85, 12, 375, 1060, 1649, 0, 0)
        ]]
    x, y = findMultiColorInRegionFuzzy(0x85877c, "-142|94|0x0d0d0d,478|-95|0x130b08,413|-97|0xa4bec9,5|-134|0xa1b8be,-15|-21|0x505753,86|-61|0x130b08,139|26|0x5d6863,125|49|0x9f9e8a,488|-7|0x221a15,75|-190|0x647377,542|-80|0x57584e,550|17|0x343f44,410|68|0x5e4d43", 85, 171, 766, 1062, 1222, 0, 0)
    if x > -1 then
        click(x, y)
        logger("主界面.牢房", LOG_DEBUG)
        return true
    end

end

function ZhuJieMian.xunFang()
    x, y = findMultiColorInRegionFuzzy(0x414543, "-730|113|0x0d0d0d,-110|-76|0x130b08,-175|-78|0xa4bec9,-583|-115|0xa1b8be,-603|-2|0x505753,-502|-42|0x130b08,-449|45|0x5d6863,-463|68|0x9f9e8a,-100|12|0x221a15,-513|-171|0x647377,-46|-61|0x57584e,-38|36|0x343f44,-178|87|0x5e4d43,19|-94|0xa3bec9,-80|-110|0x98b5bf", 85, 171, 766, 1062, 1222, 0, 0)

    if x > -1 then
        click(x, y)
        logger("主界面.寻访", LOG_DEBUG)
        return true
    end
end

function ZhuJieMian.ui()

    x, y = findMultiColorInRegionFuzzy(0x3c4a58, "155|64|0x465563,336|65|0x4f606f,337|-4|0x4e5f6e,435|-25|0x3f0e0f,384|-25|0x3f0e0f,-487|1419|0xd29b5a,-500|1387|0xd09755,-529|1388|0xb16536,-544|1434|0xb16637,-509|1441|0xd59859,-484|1419|0xc1904f,-505|1394|0xcd9655", 85, 0, 0, 1079, 1919, 0, 0)
    if x > -1 then
        logger("主界面", LOG_DEBUG)
        return true
    end
end

function ZhuJieMian.allClose()
    -- 府宅
    fz_allClose()
    fz_ui_gb()
    -- 关卡
    gq_allClose()
    gq_ui_GB()
    -- 书院
    shuYuan.close()
    -- 牢房
    laoFang.close()
    -- 寻访
    xunFang.close()
    --fuBen
    FuBen.close()
    click_kong()
		
end


function ZhuJieMian.fanHuiUi()
    wangLuoLianJieShiBai()
    jingRuYouXi()
		gongGaoGuanBi()
    flag = ZhuJieMian.ui()
    if flag then
        return
    end
    local index = 1
    flag = false;
    while not (flag) do
        logger(string.format("第%s次尝试返回主界面", index), LOG_DEBUG)
        ZhuJieMian.allClose()
        flag = ZhuJieMian.ui()
        index = index + 1
    end
end



function ZhuJieMian.run()
    local flag = false;
    logger("全局功能.运行", LOG_DEBUG)
    --[[    while not (flag) do
            flag = ZhuJieMian.ui();
            if not (flag) then
                logger(" 请切换主界面(有书院和府宅)", LOG_DEBUG)
            end
            mSleep(500)
        end]]
    sysLog("到最左面滑动")
    -- 到最左面
    szMove(  152, 383, 1027, 384)
    mSleep(200)
    szMove(  152, 383, 1027, 384)
    mSleep(200)
    szMove(  152, 383, 1027, 384)
    mSleep(2000)

    -- 副本
    FuBen.resetState()
    if zhuJieMianGN_fuBen and FuBen.needRun() then
        sysLog("判断是否有副本图标 ")
        local index = 0;
        flag=false
        -- 重试次数
        while flag == false and index < 3 do
            index = index + 1
            flag = ZhuJieMian.fuBen()
            if flag then
                ZhuJieMian.fuBen()
                FuBen.run()
            end
            mSleep(200)
        end
    end

    -- 到书院界面
    sysLog("到书院界面滑动")
    szMove(  900, 392, 300, 393)
    mSleep(200)
    szMove(  850, 392, 300, 393)
    mSleep(3000)


    for i = 1, 2 do

        -- 书院
        if zhuJieMianGN_shuYuan then
            flag = ZhuJieMian.shuYuan()
            if flag then
                ZhuJieMian.shuYuan()
                shuYuan.run()
            end
        end
        --laoFang
        --[[        if zhuJieMianGN_laoFang then
                    flag = ZhuJieMian.laoFang()
                    if flag then
                        ZhuJieMian.laoFang()
                        laoFang.run()
                    end
                end]]

        -- 寻访
        if zhuJieMianGN_xunFang then
            flag = ZhuJieMian.xunFang()
            if flag then
                ZhuJieMian.xunFang()
                xunFang.run()
            end
        end

        -- 府邸
        if zhuJieMianGN_fuZhai then
            flag = ZhuJieMian.fuZhai()
            if flag then
                ZhuJieMian.fuZhai()
                fz_run(true)
            end
        end

        -- 关卡
        if zhuJieMianGN_guanKa then
            flag = ZhuJieMian.guanQia()
            while flag do
                ZhuJieMian.guanQia()
                flag = gq_run(true)
            end
        end





        if i == 1 then
            sysLog("最右边滑动")
            -- 最右边
            szMove(  900, 392, 239, 393)
            mSleep(200)
            szMove(  900, 392, 239, 393)
            mSleep(200)
            szMove(  900, 392, 239, 393)
            mSleep(2000)

        end
        mSleep(3000)
    end
    logger("完成全局", LOG_DEBUG)
end



